<?php
class Index extends Admin_Controller {

    public function __construct() {
        parent::__construct();

        $this->_load();
    }

    public function index() {
        $this->_replacePhrase();
    }
    
    public function ajax_save() {
        $asave = $this->input->post('user');       
        $this->load->library('form_validation', null, 'validation');
        $this->validation->set_rules($this->user->get_rule(empty($asave['id'])? false: $asave['id'], 
                $asave['password'] || $asave['password_match'] ? true : false ));
        $this->validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->validation->run() === false) {
            $this->return_fail_json($this->user->get_validation_error());
            return false;
        }

        if ((bool)$asave) {
            //Save and edit in this.
            $svar_message = "phrase var='user.add_new_user_successfully'";
            if($asave['id']){
                $svar_message = "phrase var='user.update_user_successfully'";
            }
                
            if ((bool) $areturn = $this->user->save($asave)) {
                //////////////Check if choose send mail confirm.
//                if($this->mail->sendRegister()){}
                $this->return_success_json($areturn, $this->lang->line($svar_message));
                return true;
            }
        }
    }

    public function ajax_get_list($offset = null) {
        $offset = !empty($offset) ? $offset : 1;
        $aSearch = $this->input->post('search');
        $aUser = $this->user->getListAdmin($offset, $aSearch);

        //Get list of page.
        $aPage = $this->user->get_pager('user/admincp/index/ajax_get_list');
        $aPage = $this->user->get_pager_admin($aPage);

        $aReturn = array(
            'list' => $aUser,
            'pages'    => $aPage
        );

        $this->return_success_json($aReturn, '');
    }

    public function ajax_delete() {
        $aid = $this->input->post('item_ids');
        $icur_page = $this->input->post('cur_page');
        $aid = (array) json_decode($aid);

        if ((bool) $this->user->delete($aid)) {
            $aresult = $this->user->getListAdmin((int) $icur_page);
            //Prepare in case if delete all items in a page.
            if (!empty($aresult)) {
                $this->return_success_json(array('page' => $icur_page), '');
            } else {
                $this->return_success_json(array('page' => $icur_page - 1), '');
            }
            return true;
        }

        $this->return_fail_json(NULL, '');
    }

    public function ajax_get() {

        $iid = $this->input->post('item_id');
        if ((bool) $oGet = $this->user->get_by_id($iid, false)) {
            $this->return_success_json($oGet, '');

            return true;
        }

        $this->return_fail_json(NULL, '');
    }

    public function ajax_edit() {
        if (!(bool) $this->input->is_ajax_request()) {
           
            return false;
        }

        $aConfig = $this->_getRuleProduct();
        $this->load->library('form_validation', null, 'validation');
        $this->validation->set_rules($aConfig);
        $this->validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->validation->run() === false) {
            $this->return_fail_json($this->_getValidationError());

            return false;
        }

        if ((bool) $aProduct = $this->input->post('product')) {
            //Get current page. Use when reload page current after edit.
            $iCurPage = $aProduct['cur_page'];
//            $iCurPage = $aProduct['cur_page'];
            if ((bool) $aProduct = $this->product->edit($aProduct)) {
                $aProduct['cur_page'] = $iCurPage;
                $this->return_success_json($aProduct, $this->lang->line("phrase var='product.edit_product_successfully'"));
                return true;
            }
        }

        return false;
    }

    public function ajax_sort() {
        $acolumn = $this->input->post('column');
        $stype_sort = $this->user->sort($acolumn);
        $this->return_success_json(array('type_sort' => $stype_sort), '');
    }

    public function ajax_set_view() {
        $iNumView = $this->input->post('num_view');

        if ($iNumView) {
            $this->session->set_userdata($this->config->item('ele_per_page'), $iNumView);
            $this->return_success_json(array('num_view' => $iNumView), '');

            return true;
        }

        $this->return_fail_json(array('num_view' => $iNumView), '');
    }

    public function getSort() {
        $this->_sort = $this->session->userdata($this->config->item('sort'));

        if (!(bool) $this->_sort) {
            return array('order' => 'asc');
        }
    }

    private function _load() {
        $this->load->library('pagination');
        $this->load->helper(array('url', 'form'));
        $this->load->model(array('user'));
        $this->load->model('mail', 'mail', false, 'mail');
        $this->lang->load('admin/general', $this->_slang_name);
        $this->lang->load('admin/user', $this->_slang_name);
    }

    private function _replacePhrase() {
        $this->_adata['token'] = (object) array("name" => $this->security->get_csrf_token_name(), "value" => $this->security->get_csrf_hash());
        //Get sort of product.
        $this->_adata['aSort'] = $this->user->getSort();
        $this->_adata['phrase_js'] = json_encode($this->lang->language);
        $this->fill_phrase('index', $this->_adata, $this->lang->language);
    }
}
?>
